#include <iostream>
#include<string.h>
using namespace std;
class Student{
private:
    char* name;
    int id;
    int age;
public:
    Student(char*Studentname,int Studentid,int Studentage)
    :id(Studentid)
    ,age(Studentage)
    {
    name=new char(strlen(Studentname)+1);
    strcpy(name,Studentname);
    cout<<"构造函数"<<endl;
    }
    //析构函数
    ~Student(){
        delete[] name;
        cout<<"析构函数被调用"<<endl;
    }
    //拷贝构造函数,为了避免浅拷贝带来的double free问题
    Student(const Student & other){
        id=other.id;
        age=other.age;
        name=new char(strlen(other.name)+2);
        strcpy(name,other.name);
        cout<<"拷贝构造函数被调用"<<endl;
    } 


    //赋值运算符函数
    Student & operator=(const Student& other){
    if(this!=&other){
    delete[] name;

    id=other.id;
    age=other.age;
    name=new char (strlen(other.name));
    strcpy(name,other.name);
    cout<<"赋值运算符函数被调用"<<endl;
    } 
    return *this;
    }
    void display(){
        cout<<"name"<<name<<"id"<<id<<"age"<<age<<endl;
    }
};

void test(void){
    char*name1="zs";
    Student s1(name1,1,2);
    Student s2=s1;
    s1.display();
    s2.display();
    char *name2="ls";
    Student s3(name2,3,4);
    s2=s3;
    s2.display();
    s3.display();
}
int main(int argc, char * argv[]){
    test();
    return 0;
}

